import * as vscode from 'vscode';
import { deleteGitBranch, getAllGitBranches, getSubtreeBranches, switchGitBranch } from '../../utils/git';
import { getConfig } from './subtree';


export class GitBranchTreeItem extends vscode.TreeItem {
  constructor(
    public readonly label: string,
    public readonly collapsibleState: vscode.TreeItemCollapsibleState,
    public contextValue?: string, // 定义上下文值
    iconPath?: string | vscode.ThemeIcon, // 图标路径
  ) {
    super(label, collapsibleState);
    this.iconPath = iconPath;
  }
}

export class SubtreeViewProvider implements vscode.TreeDataProvider<GitBranchTreeItem> {
  private _onDidChangeTreeData: vscode.EventEmitter<GitBranchTreeItem | undefined> = new vscode.EventEmitter<GitBranchTreeItem | undefined>();
  readonly onDidChangeTreeData: vscode.Event<GitBranchTreeItem | undefined> = this._onDidChangeTreeData.event;

  /** 是否显示远程分支 */
  public showRemote = false;

  refresh(): void {
    this._onDidChangeTreeData.fire(undefined);
  }

  getTreeItem(element: GitBranchTreeItem): vscode.TreeItem {
    return element;
  }

  getChildren(element?: GitBranchTreeItem): Thenable<GitBranchTreeItem[]> {
    if (!element) {
      // 返回根节点
      const { subtrees } = getConfig();

      return Promise.resolve(subtrees?.map((subtree:any) => {
        return new GitBranchTreeItem(subtree.name, vscode.TreeItemCollapsibleState.Collapsed, 'subtree', new vscode.ThemeIcon('git-pull-request-abandoned'));
      }));
    }

    return this.getSubtreeChildren(element);
  }

  toggleNodeState(node: GitBranchTreeItem): void {
    node.contextValue = node.contextValue === 'default' ? 'activated' : 'default';
    this.refresh();
  }

  private async getSubtreeChildren(element: GitBranchTreeItem): Promise<GitBranchTreeItem[]> {
    const workspaceFolders = vscode.workspace.workspaceFolders;
    const config = getConfig();
    const subtree = config.subtrees.find((subtree: any) => subtree.name === element.label);

    const res = await getSubtreeBranches(subtree.url) ?? [];

    console.log('res', res);

    return Promise.resolve(res?.map((branch: any) => {
      let label = branch;

      if (branch === subtree.branch) {
        label = '(当前)' + branch;
      }
      return new GitBranchTreeItem(label, vscode.TreeItemCollapsibleState.None, 'subtree', new vscode.ThemeIcon('git-pull-request-abandoned'));
    }));
  }

}


export function registerView(context: vscode.ExtensionContext) {
  const subtreeViewProvider = new SubtreeViewProvider();

  vscode.window.registerTreeDataProvider('zd.subtree', subtreeViewProvider);

  context.subscriptions.push(
    vscode.commands.registerCommand('zd-vscode-tools.subtree.pull', async (node: GitBranchTreeItem) => {
      const workspaceFolders = vscode.workspace.workspaceFolders;

      console.log('pull', node);


    })
  );

  context.subscriptions.push(
    vscode.commands.registerCommand('zd-vscode-tools.subtree.push', async (node: GitBranchTreeItem) => {
      const workspaceFolders = vscode.workspace.workspaceFolders;

      console.log('push', node);
    })
  );
}


